package asionreachel.cn.smarthomedemo.activity;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

import asionreachel.cn.smarthomedemo.R;
import asionreachel.cn.smarthomedemo.adapter.CDeviceListAdapter;
import asionreachel.cn.smarthomedemo.base.BaseDevice;
import asionreachel.cn.smarthomedemo.base.Common;
import asionreachel.cn.smarthomedemo.base.ConstantDefinition;
import asionreachel.cn.smarthomedemo.base.ItemDialog;
import asionreachel.cn.smarthomedemo.data.CDeviceItem;
import asionreachel.cn.smarthomedemo.device.SlidingMenuMgr;
import asionreachel.cn.smarthomedemo.listener.OnRecyleItemListener;


/**
 * Created by SuperMa on 2017/6/24.
 */

public class DeviceListActivity {
    public static Activity mActivity = null;
    public static DeviceListActivity mDeviceListActivity;
    private LinkedList<CDeviceItem> mListItems;
    public ListView mPullListView;
    public void onCreate(Activity _tp, View view) {
        mActivity = _tp;
        mPullListView = (ListView)view.findViewById(R.id.fragent_device_list);
        mListItems = new LinkedList<CDeviceItem>();

        CDeviceListAdapter adapter = new CDeviceListAdapter(mActivity.getApplicationContext(), R.layout.device_list_item ,mListItems);
        mPullListView.setAdapter(adapter);
        adapter.setOnRecyleItemListener(new OnRecyleItemListener() {
            @Override
            public void onItemClick(int position) {
                //点击列表回调
                SlidingMenuMgr.getInstance().setSelectPosition(position);
                Intent intent = BaseDevice.GetIntent( mActivity , mListItems.get(position).getDeviceId() );
                mActivity.startActivity(intent);
                mActivity.finish();
            }
        });
        adapter.setOnLongRecyItemListener(new OnRecyleItemListener() {
            @Override
            public void onItemClick(int position) {
                CDeviceItem clickDeviceItem = mListItems.get(position);
                JSONObject object = new JSONObject();
                try {
                    object.put(ConstantDefinition.EDIT_PARM, clickDeviceItem.getDeviceId());
                    object.put(ConstantDefinition.EDIT_CONTENT , clickDeviceItem.getName());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                showItemDialog(mActivity ,mListItems.get(position).getName() , object.toString());
            }
        });
    }

    public void addDeviceItem(String a_strDeviceId, String a_strName, boolean a_iOnline, int a_iIconOnline, int a_iIconOffline) {
        addDeviceItem( new CDeviceItem( a_strDeviceId, a_strName, a_iOnline, a_iIconOnline, a_iIconOffline ) );
    }

    public void addDeviceItem( CDeviceItem a_itemDevice ) {
        if (mActivity == null)
            return;

        int position = SlidingMenuMgr.getInstance().getSelectPosition();
        for( int iIndex = 0 ; iIndex < mListItems.size() ; ++iIndex )
        {
            if( mListItems.get( iIndex ).getDeviceId().equals( a_itemDevice.getDeviceId() ) ) {
                mListItems.set( iIndex, a_itemDevice );
                if( position == iIndex )
                    a_itemDevice.setIsSelect(true);
                return;
            }
        }
        if (position == mListItems.size())
            a_itemDevice.setIsSelect(true);
        mListItems.add(a_itemDevice);
    }

    //添加完数据需要更新
    public void notifyDataChanged() {
        if (mActivity == null)
            return;
        CDeviceListAdapter adapter = (CDeviceListAdapter) mPullListView.getAdapter();
        adapter.notifyDataSetChanged();
    }

    //清空设备数据
    public void clearDeviceItems() {
        if (mActivity == null)
            return;
        mListItems.clear();
    }

    public static DeviceListActivity getInstance()
    {
        if (mDeviceListActivity == null)
            mDeviceListActivity = new DeviceListActivity();
        return mDeviceListActivity;
    }


    private static final String DIALOG_SET_MODIFY_NAME = "修改名称";
    private static final String DIALOG_SET_CANCEL_BANDING = "解除绑定";
    private static final String DIALOG_SET_RESTORE_FACTORY = "恢复出厂";
//    private static final String DIALOG_SET_SHARE_INFO = "分享";
    private static final String[] TOPBAR_ITEM_NAMES = {DIALOG_SET_MODIFY_NAME, DIALOG_SET_CANCEL_BANDING, DIALOG_SET_RESTORE_FACTORY};
    private static final int DIALOG_SET_DEVICE_FUNC = 1;
    /**显示列表选择弹窗
     */
    public static void showItemDialog(Activity _tp , String a_strTitle , String a_jsonDeviceInfo) {
        new ItemDialog(_tp, TOPBAR_ITEM_NAMES, a_strTitle, DIALOG_SET_DEVICE_FUNC, mDialogItemListener , a_jsonDeviceInfo).show();
    }

    private static ItemDialog.OnDialogItemClickListener mDialogItemListener = new ItemDialog.OnDialogItemClickListener() {
        @Override
        public void onDialogItemClick(int requestCode, int position, String item , String a_strParm) {
            if (position < 0) {
                position = 0;
            }
            switch (requestCode) {
                case DIALOG_SET_DEVICE_FUNC:
                    if (position >= TOPBAR_ITEM_NAMES.length) {
                        position = TOPBAR_ITEM_NAMES.length - 1;
                    }
                    String value = TOPBAR_ITEM_NAMES[position];

                    switch (value)
                    {
                        case DIALOG_SET_MODIFY_NAME:        //修改名称
                            try {
                                JSONObject jsonObject = new JSONObject(a_strParm);
                                String strDeviceId = jsonObject.optString(ConstantDefinition.EDIT_PARM , "");
                                String strDeviceName = jsonObject.optString(ConstantDefinition.EDIT_CONTENT , "");
                                Common.openEditTextInfoActivity(mActivity  , 0, strDeviceId , strDeviceName , 0 , "修改名称");
                                EditTextInfoActivity.setEditItemClickListener(new EditTextInfoActivity.OnEditItemClickListener() {
                                    @Override
                                    public void onEditItemClick(int requestCode, String a_strContent, String a_strParm) {
                                        //修改设备名称成功
                                        Log.d("DIALOG_SET_MODIFY_NAME" , a_strContent);
                                        BaseDevice.SetDeviceName( a_strParm, a_strContent );
                                    }
                                });
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            break;
                        case DIALOG_SET_CANCEL_BANDING:
                            try {
                                JSONObject jsonObject = new JSONObject(a_strParm);
                                String strDeviceId = jsonObject.optString(ConstantDefinition.EDIT_PARM , "");
                                new AlertDialog(mActivity, "取消绑定", "确定取消绑定？", strDeviceId, true, 0, m_OnDialogListener).show();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            break;
                        case DIALOG_SET_RESTORE_FACTORY:
                            try {
                                JSONObject jsonObject = new JSONObject(a_strParm);
                                String strDeviceId = jsonObject.optString(ConstantDefinition.EDIT_PARM , "");
                                new AlertDialog(mActivity, "恢复出厂", "确定恢复出厂？", strDeviceId, true, 1, m_OnDialogListener).show();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            break;
//                        case DIALOG_SET_SHARE_INFO:
//                            try {
//                                JSONObject jsonObject = new JSONObject(a_strParm);
//                                String strDeviceID = jsonObject.optString(ConstantDefinition.EDIT_PARM , "");
//                                Intent intent = new Intent( mActivity , ShareListActivity.class );
//                                intent.putExtra( "DeviceID", strDeviceID );
//                                mActivity.startActivity(intent);
//                            } catch (JSONException e) {
//                                e.printStackTrace();
//                            }
//                            break;
                        default:
                            break;

                    }
                    break;
                default:
                    break;
            }
        }
    };

    private static AlertDialog.OnDialogButtonClickListener m_OnDialogListener = new AlertDialog.OnDialogButtonClickListener()
    {
        @Override
        public void onDialogButtonClick(int requestCode, boolean isPositive, String a_strParam) {
            if( !isPositive )
                return;

            switch (requestCode)
            {
                case 0:     //取消绑定
                    BaseDevice.SetFamily( a_strParam, "" );
                    break;
                case 1:     //恢复出厂
                    BaseDevice.ResetDevice( a_strParam );
                    break;
                default:
                    break;
            }
        }
    };
}
